/**
 * Copyright 2017 by FREEMYPAL. Tianhua Information Technology Park (South) Building 4, 4th Floor, 
 * Longtian Road 195,Shanghai, P.R. China，200235. All rights reserved.
 *
 * This software is the confidential and proprietary information of FREEMYPAL
 * ("Confidential Information"). You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with FREEMYPAL.
 */
package com.roc.web.action.agent;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.opensymphony.xwork2.ActionContext;
import com.roc.newPage.IPageService;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.pojo.OperatorsMchtStatisticsVo;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;

/**
 * @Description: 代理商统计信息查询控制器类
 * @author Jian Jang
 * @version 1.0 2017年7月28日
 */
@Component
@Namespace("/agencyMerSummary")
@SuppressWarnings("serial")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class AgencyMerSummaryAction extends PaginationBaseAction {

	private static Logger logger = Logger.getLogger(AgencyMerSummaryAction.class);

	private AgencyMerSummarySearchCondition searchCondition;
	@Autowired
	private IPageService pagenitionService;

	/**
	 * 初始化页面
	 * @return
	 */
	@Action(value = "initPage", results = {
			@Result(name = "success", location = "/pages/agent/agentMerSummary.jsp"),
			@Result(name = "error", location = "/error.jsp") })
	public String initPage() {
		Operators op = (Operators) ActionContext.getContext().getSession()
				.get(Constant.SESSION_LOGIN_MERCHANT);
		try {
			if (!op.getRoleType().equals(Role.AGENT.getCode())
					&& !op.getRoleType().equals(Role.SYSTEM.getCode())
					&& !op.getRoleType().equals(Role.O_MANAGER.getCode())) {
				addActionError("此登陆账号没有权限");
				return ERROR;
			}

		} catch (Exception e) {
			logger.error("查询代理商商户统计页面初始化异常", e);
			addActionError("查询代理商商户统计页面初始化异常");
		}
		return SUCCESS;
	}

	@Action(value = "queryAgencyMerSummary", results = {
			@Result(name = "success", location = "/pages/agent/agentMerSummary.jsp"),
			@Result(name = "error", location = "/error.jsp") })
	public String queryAgencyMerSummary() {
		// 获取登录人员信息
		Operators logingMerchant = (Operators) getSession().getAttribute(
				Constant.SESSION_LOGIN_MERCHANT);
		// 判断查询条件是否为空
		if (searchCondition == null) {
			searchCondition = (AgencyMerSummarySearchCondition) getSearchConditionFromSession();
			if (searchCondition == null) {
				searchCondition = new AgencyMerSummarySearchCondition();
			}
		}
		if (StringUtils.isBlank(searchCondition.getMinDate())
				|| StringUtils.isBlank(searchCondition.getMaxDate())) {
			logger.info("代理商商户统计信息查询查询时间为空");
			addActionError("代理商商户统计信息查询查询时间为空");
			return SUCCESS;
		}
		try {
			// 添加查询条件
			if (Role.AGENT.getCode().equals(logingMerchant.getRoleType()) || Role.O_MANAGER.getCode().equals(logingMerchant.getRoleType()) ) {
				// 代理商只能查询自己的信息
				searchCondition.setAgentId(logingMerchant.getId() + "");
			}
			searchCondition.setRoleType(logingMerchant.getRoleType());
			int rows = pagenitionService.countSQL(searchCondition);
			if (rows > 0) {
				searchRsList = pagenitionService.findAllSQL(searchCondition,
						calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
				dealWithTheListSpecially(searchRsList);
				List<OperatorsMchtStatisticsVo> list = new ArrayList<OperatorsMchtStatisticsVo>();
				if (!CollectionUtils.isEmpty(searchRsList)) {
					Iterator<?> it = searchRsList.iterator();
					OperatorsMchtStatisticsVo operatorsMchtStatistics = null;
					while (it.hasNext()) {
						Object[] obj = (Object[]) it.next();
						operatorsMchtStatistics = new OperatorsMchtStatisticsVo();
						operatorsMchtStatistics.setId(Constance.Null2Zero(obj[0]));
						operatorsMchtStatistics.setAgentId(Constance.Null2EmptyTrim(obj[1]));
						operatorsMchtStatistics.setAgentName(Constance.Null2EmptyTrim(obj[2]));
						operatorsMchtStatistics.setAddMchtNum(Constance.Null2Zero(obj[3]));
						operatorsMchtStatistics.setCheckMchtNum(Constance.Null2Zero(obj[4]));
						operatorsMchtStatistics.setNoBindingSnNum(Constance.Null2Zero(obj[5]));
						operatorsMchtStatistics.setBriskMchtNum(Constance.Null2Zero(obj[6]));
						operatorsMchtStatistics.setTransAmt(Constance.Null2Zero(obj[7]));
						operatorsMchtStatistics.setTransNum(Constance.Null2Zero(obj[8]));
						operatorsMchtStatistics.setMonthBriskMchtNum(Constance.Null2Zero(obj[9]));
						operatorsMchtStatistics.setDormentMchtNum(Constance.Null2Zero(obj[10]));
						operatorsMchtStatistics.setExcellentMchtNum(Constance.Null2Zero(obj[11]));
						operatorsMchtStatistics.setSummaryDate(Constance.Null2EmptyTrim(obj[12]));
						operatorsMchtStatistics.setAccountNo(Constance.Null2EmptyTrim(obj[13]));
						list.add(operatorsMchtStatistics);
					}
					searchRsList.clear();
					searchRsList.addAll(list);
				}

			}
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			addActionError(e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass() throws Exception {
		return searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(BaseSearchCondition searchCondition)
			throws Exception {
		this.setSearchConditionToSubClass(searchCondition);

	}

	@Override
	protected void setSearchToConditionSession(BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_AGENCY_MCHT_SUMMARY_QUERY, searchCondition);

	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		return (AgencyMerSummarySearchCondition) getSession().getAttribute(
				SEARCH_AGENCY_MCHT_SUMMARY_QUERY);
	}

	@Override
	protected String getInitPageSize() {
		return Constance.PAGE_MAX_SIZE + "";
	}

	public AgencyMerSummarySearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(AgencyMerSummarySearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}

}
